
import { put, takeEvery } from 'redux-saga/effects'
import apiType, {
    EDIT_SCHEDULE_APPLY,
    EDIT_SCHEDULE_RECEIVE,
    EDIT_SCHEDULE_REVIEW,
    GET_WAREHOUSE_SCHEDULE_IN,
    GET_WAREHOUSE_SCHEDULE_OUT,
    GET_WAREHOUSE_SCHEDULE_INFO
} from "@constants/ActionTypes";
import {getAxios, postAxios, putAxios, deleteAxios} from "@utils/api";

// 创建调拨申请单
export const creatScheduleApply = (params, callback) => ({
    type: apiType(EDIT_SCHEDULE_APPLY).request,
    params,
    callback
})
// 调拨申请单入库
export const creatScheduleReceive = (params, callback) => ({
    type: apiType(EDIT_SCHEDULE_RECEIVE).request,
    params,
    callback
})
// 调拨申请单调出审核
export const creatScheduleReview = (params, callback) => ({
    type: apiType(EDIT_SCHEDULE_REVIEW).request,
    params,
    callback
})
// 调入管理列表
export const getWarehouseScheduleIn = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_SCHEDULE_IN).request,
    params,
    callback
})
// 调出管理列表
export const getWarehouseScheduleOut = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_SCHEDULE_OUT).request,
    params,
    callback
})
// 调拨单详情
export const getWarehouseScheduleInfo = (params, callback) => ({
    type: apiType(GET_WAREHOUSE_SCHEDULE_INFO).request,
    params,
    callback
})

const sagas = {
    * [apiType(EDIT_SCHEDULE_APPLY).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/schedule/apply', params)
            yield put({ type: apiType(EDIT_SCHEDULE_APPLY).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(EDIT_SCHEDULE_APPLY).failure});
        }
    },
    * [apiType(EDIT_SCHEDULE_RECEIVE).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/schedule/receive', params)
            yield put({ type: apiType(EDIT_SCHEDULE_RECEIVE).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(EDIT_SCHEDULE_RECEIVE).failure});
        }
    },
    * [apiType(EDIT_SCHEDULE_REVIEW).request]  ({ params, callback }) {
        try {
            const response = yield postAxios('/inventory/web/schedule/review', params)
            yield put({ type: apiType(EDIT_SCHEDULE_REVIEW).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(EDIT_SCHEDULE_REVIEW).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_SCHEDULE_IN).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/schedule/in', params)
            yield put({ type: apiType(GET_WAREHOUSE_SCHEDULE_IN).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_SCHEDULE_IN).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_SCHEDULE_OUT).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/schedule/out', params)
            yield put({ type: apiType(GET_WAREHOUSE_SCHEDULE_OUT).success, response });
            callback && callback()
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_SCHEDULE_OUT).failure});
        }
    },
    * [apiType(GET_WAREHOUSE_SCHEDULE_INFO).request]  ({ params, callback }) {
        try {
            const response = yield getAxios('/inventory/web/schedule/info', params)
            yield put({ type: apiType(GET_WAREHOUSE_SCHEDULE_INFO).success, response });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_WAREHOUSE_SCHEDULE_INFO).failure});
        }
    },

}
export default Object.keys(sagas).map(item => {
    return function * s() {
        yield takeEvery(item, function *(args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})